﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Commonlib;
namespace AlbonWeb.Order
{
    public partial class OrderItemList : System.Web.UI.Page
    {
        private readonly string username = HttpContext.Current.User.Identity.Name;
        private BLL.ItemBLL itembll = new BLL.ItemBLL();
        private readonly BLL.OrderBLL orderbll = new BLL.OrderBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {  
                //页面初始化
                if (Request["orderid"] != null)
                {
                    //如果是修改已完成的订单，则将orderid存入session，方便更改订单信息后重新加载订单列表页面
                    Session["orderid"] = Request["orderid"].ToString();
                }
                else {
                    //如果是新添加的未完成订单，则将该订单ID放入session
                    Session["orderid"] = orderbll.getNewOrderID(username);
                }
                initData();
                bindRep();
            }
            
        }
        protected void initData()
        {
            DataTable dt = orderbll.getOrderByID(Session["orderid"].ToString());
            //单号
            refNo.Text = dt.Rows[0]["refNo"].ToString();
            //客户名
            clientName.Text = dt.Rows[0]["clientName"].ToString();
            //绑定Item列表
            ddlItem.DataSource = itembll.getAllItem();
            ddlItem.DataTextField = "itemName";
            ddlItem.DataValueField = "ID";
            ddlItem.DataBind();
            ddlItem.Items.Insert(0,new ListItem("--select item--","0"));
            
            
        }
        
        //绑定已完成的订单项
        protected void bindRep()
        {
            string cmdstr = "select * from OrderItemView where orderID="+Session["orderid"].ToString()+" and finished=1";
            SqlHelper.BindRepeater(repMain, SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //将所选的itemID传递到下一个页面
            Response.Redirect("/Order/ItemSize.aspx?itemid="+ddlItem.SelectedValue);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            //判断订单内是否有订单项
            if (orderbll.hasItems(Session["orderid"].ToString()))
            {
                Response.Redirect("/Order/ShipAndPay.aspx");
            }
            else
            {
                lbmessage.Text = "Can not finish a empty order!";
            }
        }

        //Copy Order
        protected void imgDelete_Click(object sender, ImageClickEventArgs e)
        { 
            //Delete Item数据
            ImageButton imgDelete=(ImageButton)sender;
            string orderitemid = imgDelete.CommandArgument;
            BLL.OrderItemBLL orderitembll = new BLL.OrderItemBLL();
            orderitembll.deleteOrderItem(orderitemid);
            bindRep();
        }
        
    }
}